﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using log4net;
using System.Net.Http;
using System.Net.Http.Headers;
using SharpConfig;
using System.Threading;

namespace SearchFileIformation
{
    public partial class ShowForm : Form
    {
        public delegate void ShowContentArgs(SearchFileEventArgs e);
        ShowContentArgs myShowContent;
        SearchFile mySearchFile = new SearchFile();
        Thread myThread;
        log4net.ILog log = log4net.LogManager.GetLogger("Logger");
        Dictionary<int, bool> flagCheckStatu = new Dictionary<int, bool>();
        int count = 1;
        public ShowForm()
        {
            InitializeComponent();
            listViewSelect.View = System.Windows.Forms.View.Details;
            listViewStatu.View = System.Windows.Forms.View.Details;
            mySearchFile.searchFileEvent = new SearchFile.SearchFileEvent(run);
            myShowContent = new ShowContentArgs(showContent);
        }

        private void buttonChoosePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog open = new FolderBrowserDialog();
            open.ShowDialog();
            textBoxShowFilePath.Text = open.SelectedPath;
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            log.Info(this);
            listViewSelect.Clear();
            listViewStatu.Clear();
            string[] menu = mySearchFile.getMenu();
            string[] statu = mySearchFile.getStatu();
            if (menu.Length != 0)
            {
                ListViewItem listViewItem = new ListViewItem();
                ColumnHeader columnHeaderFrame = new ColumnHeader();
                columnHeaderFrame.Text = "selectframe";
                columnHeaderFrame.Width = 100;
                columnHeaderFrame.TextAlign = HorizontalAlignment.Center;
                listViewSelect.Columns.Add(columnHeaderFrame);
                foreach (string showMenu in menu)
                {
                    ColumnHeader columnHeader = new ColumnHeader();
                    columnHeader.Text = showMenu;
                    columnHeader.Width = 100;
                    columnHeader.TextAlign = HorizontalAlignment.Center;
                    listViewSelect.Columns.Add(columnHeader);
                }
                foreach (string showStatu in statu)
                {
                    ColumnHeader columnHeader = new ColumnHeader();
                    columnHeader.Text = showStatu;
                    columnHeader.Width = 200;
                    columnHeader.TextAlign = HorizontalAlignment.Center;
                    listViewStatu.Columns.Add(columnHeader);
                }
            }
            myThread = new Thread(new ParameterizedThreadStart(mySearchFile.runSearch));
            myThread.Start(textBoxShowFilePath.Text);
        }
        public void run(Object sender, SearchFileEventArgs e)
        {
            if (e.getException() != null)
                log.Error(this, e.getException());
            else
                log.Info("");
            if (this.InvokeRequired)
                this.BeginInvoke(myShowContent, e);

        }
        public void showContent(SearchFileEventArgs e)
        {
            string[] menu = e.getMenu();
            try
            {
                List<Dictionary<string, string>> showText = e.getListContent();
                foreach (Dictionary<string, string> show in showText)
                {
                    ListViewItem listViewItem = new ListViewItem();
                    for (int i = 0; i < show.Count; i++)
                    {
                        listViewItem.SubItems.Add(show[menu[i]]);
                    }
                    listViewSelect.Items.Add(listViewItem);
                    ListViewItem listViewItemStatu = new ListViewItem(DateTime.Now.ToString());
                    listViewItemStatu.SubItems.Add(count++.ToString());
                    if (mySearchFile.GetResponseJson(show["api"]).StartsWith("error"))
                        listViewItemStatu.SubItems.Add("false");
                    else
                        listViewItemStatu.SubItems.Add("success");
                    listViewItemStatu.SubItems.Add("");
                    listViewStatu.Items.Add(listViewItemStatu);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                log.Error(this, ex);
            }
        }
        private void listViewSelect_ItemChecked(object sender,ItemCheckedEventArgs e)
        {
            
            int index = mySearchFile.getApiIndex();
            try
            {
                int i = 0;
                foreach (ListViewItem listViewItem in listViewSelect.Items)
                {
                    if (listViewItem.Checked)
                    {
                        if (!flagCheckStatu[i])
                        {
                            flagCheckStatu.Remove(i);
                            flagCheckStatu.Add(i, true);
                            listViewStatu.Items[i].SubItems[3].Text = mySearchFile.GetResponseJson(listViewItem.SubItems[1].Text);
                        }                                  
                    }
                    else
                    {
                        flagCheckStatu.Remove(i);
                        flagCheckStatu.Add(i, false);
                        if (listViewStatu.Items.Count==flagCheckStatu.Count)
                            listViewStatu.Items[i].SubItems[3].Text = "";  
                    }                               
                    i++;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                log.Error(this, ex);
            }    
        }
    }
}